package com.szbmyoubao.app.ui.preview

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.widget.ViewPager2
import com.szbmyoubao.app.adapter.MyFragmentStateAdapter
import com.szbmyoubao.app.view.dialog.ReportDialog
import com.ellecity06.common.R
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.base.BaseViewModel
import com.ellecity06.cwhiskymall.databinding.ActivityPreviewImageVideoBinding
import com.gyf.immersionbar.ktx.immersionBar


/**
 * @author ellecity06
 * @time 2025/3/16 16:13
 * @des
 */
class PreviewImageActivity :
    BaseMvvmActivity<ActivityPreviewImageVideoBinding, BaseViewModel>() {
    private val list: List<String> by lazy {
        intent.getStringArrayListExtra(LIST_DATA) ?: arrayListOf()
    }

    companion object {
        const val LIST_DATA = "list_data"
        fun launch(activity: FragmentActivity, list: ArrayList<String>) {
            activity.startActivity(
                Intent(
                    activity,
                    PreviewImageActivity::class.java
                ).apply {
                    putStringArrayListExtra(LIST_DATA, list)
                })

        }
    }

    override fun initImmersionBar() {
        immersionBar {
            transparentBar()
            statusBarDarkFont(darkFont)
            navigationBarColor(R.color.white)
            navigationBarEnable(false)
            navigationBarWithKitkatEnable(false)
            titleBar(binding.toolBar)
        }
    }

    override val darkFont: Boolean
        get() = false
    override val userBaseTitle: Boolean
        get() = false

    override fun inflateBinding(): ActivityPreviewImageVideoBinding {
        return ActivityPreviewImageVideoBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        initViewPager()
    }

    private fun initViewPager() {
        val toMutableList = list.map { PreviewImageFragment.newInstance(it) }.toMutableList()
        val myFragmentStateAdapter = MyFragmentStateAdapter(toMutableList, this)
        binding.viewPager.adapter = myFragmentStateAdapter
        binding.viewPager.offscreenPageLimit = toMutableList.size
        binding.viewPager.currentItem = 0


    }

    private var index = 0

    // 2. 创建监听回调对象
    private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            index = position
            // 页面切换完成时调用
            binding.tvTitle.text = "${position + 1}/${list.size}"
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            // 滑动过程中持续调用
        }

        override fun onPageScrollStateChanged(state: Int) {
            // 滑动状态变化时调用
        }
    }

    override fun initEvent() {
        binding.ivBack.setOnClickListener { finish() }
        binding.viewPager.registerOnPageChangeCallback(pageChangeCallback)
        binding.tvTitleRight.setOnClickListener {
            ReportDialog(list[index], false).show(supportFragmentManager, "ReportDialog")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.viewPager.unregisterOnPageChangeCallback(pageChangeCallback)
    }
}